package studentStatus.view;

import studentStatus.constants.Sqls;
import studentStatus.tools.DatasourceUtils;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Win extends JFrame implements Sqls {


	private JPanel contentPane;
	private JTextField jt_name;
	private JTextField jt_own;
	private JTextField jt_id;
	private JTextField jt_sex;
	private JTextField jt_time;
	private JTextField jt_eval;
	private JTable table;
	private int isSelected = 0;

	Object[][] data;




	void init() {
		try {
			ResultSet set = DatasourceUtils.query(sql_all, new Object[]{});
			data = DatasourceUtils.getSetArrays(set);
			table.setModel(new DefaultTableModel(data, columnNames));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	void clearAll(){
		jt_id.setText("");
		jt_name.setText("");
		jt_eval.setText("");
		jt_sex.setText("");
		jt_time.setText("");
		jt_own.setText("");
	}

	/**
	 * Create the frame.
	 */
	public Win() {
		setTitle("\u5B66\u7C4D\u7BA1\u7406\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 721, 542);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 10, 685, 483);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u59D3\u540D");
		lblNewLabel.setBounds(144, 10, 54, 15);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u7C4D\u8D2F");
		lblNewLabel_1.setBounds(144, 46, 54, 15);
		panel.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("\u5B66\u53F7");
		lblNewLabel_2.setBounds(144, 88, 54, 15);
		panel.add(lblNewLabel_2);
		
		JLabel lblNewLabel_1_1 = new JLabel("\u5165\u5B66\u65F6\u95F4");
		lblNewLabel_1_1.setBounds(285, 46, 54, 15);
		panel.add(lblNewLabel_1_1);
		
		JLabel lblNewLabel_3 = new JLabel("\u6027\u522B");
		lblNewLabel_3.setBounds(307, 10, 54, 15);
		panel.add(lblNewLabel_3);
		
		JLabel lblNewLabel_2_1 = new JLabel("\u671F\u672B\u8BC4\u4F30");
		lblNewLabel_2_1.setBounds(285, 88, 54, 15);
		panel.add(lblNewLabel_2_1);
		
		jt_name = new JTextField();
		jt_name.setBounds(174, 4, 100, 21);
		panel.add(jt_name);
		jt_name.setColumns(10);
		
		jt_own = new JTextField();
		jt_own.setBounds(174, 40, 100, 21);
		panel.add(jt_own);
		jt_own.setColumns(10);
		
		jt_id = new JTextField();
		jt_id.setBounds(174, 82, 100, 21);
		panel.add(jt_id);
		jt_id.setColumns(10);
		
		jt_sex = new JTextField();
		jt_sex.setBounds(337, 4, 100, 21);
		panel.add(jt_sex);
		jt_sex.setColumns(10);
		
		jt_time = new JTextField();
		jt_time.setBounds(337, 40, 100, 21);
		panel.add(jt_time);
		jt_time.setColumns(10);
		
		jt_eval = new JTextField();
		jt_eval.setBounds(337, 82, 100, 21);
		panel.add(jt_eval);
		jt_eval.setColumns(10);
		
		JButton jb_reset = new JButton("\u6E05\u7A7A");
		jb_reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clearAll();
				init();
			}
		});
		jb_reset.setFont(new Font("宋体", Font.PLAIN, 15));
		jb_reset.setBounds(466, 4, 93, 48);
		panel.add(jb_reset);

		JButton jb_note = new JButton("公告");
		jb_note.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,"学生学籍信息管理系统正式上线，方便管理各类重要信息。请尽快登录系统，完成必要的操作。祝学习顺利！\n" +
						"\n" +
						"学校教务部");
			}
		});
		jb_note.setFont(new Font("宋体", Font.PLAIN, 15));
		jb_note.setBounds(566, 4, 93, 48);
		panel.add(jb_note);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 123, 665, 276);
		panel.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);

		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) { // 确保事件不会被重复触发
					int row = table.getSelectedRow();

					if (row != -1) {
						isSelected = (int) table.getValueAt(row, 1);
						// 获取选中行的数据
						jt_name.setText(table.getValueAt(row, 3).toString());
						jt_sex.setText((String) table.getValueAt(row, 4));
						jt_own.setText((String) table.getValueAt(row, 5));
						jt_time.setText(table.getValueAt(row, 6)+"");
						jt_eval.setText((String) table.getValueAt(row, 7));
						jt_id.setText(table.getValueAt(row, 2)+"");
					}
				}
			}
		});

		init();
		
		JButton jb_find = new JButton("\u67E5\u8BE2");
		jb_find.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nameText = jt_name.getText();
				String sexText = jt_sex.getText();
				String ownText = jt_own.getText();
				String timeText = jt_time.getText();
				String idText = jt_id.getText();
				String evalText = jt_eval.getText();

				String sql5_search_plus = "SELECT * FROM student WHERE 1 = 1 ";

				ArrayList<Object> list = new ArrayList<>();
				if (!idText.equals("")) {
					sql5_search_plus += "and num= ? ";
					System.out.println(sql5_search_plus);
					list.add(idText);
				}
				if (!nameText.equals("")) {
					sql5_search_plus += "and name like ? ";
					System.out.println(sql5_search_plus);
					list.add(nameText+"%");
				}
				if (!sexText.equals("")) {
					sql5_search_plus += "and gender= ? ";
					System.out.println(sql5_search_plus);
					list.add(sexText);
				}
				if (!ownText.equals("")) {
					sql5_search_plus += "and hometown= ? ";
					System.out.println(sql5_search_plus);
					list.add(ownText);
				}
				if (!timeText.equals("")) {
					sql5_search_plus += "and date= ? ";
					System.out.println(sql5_search_plus);
					list.add(timeText);
				}
				if (!evalText.equals("")) {
					sql5_search_plus += "and evaluation= ? ";
					System.out.println(sql5_search_plus);
					list.add(evalText);
				}

				try {
					System.out.println(sql5_search_plus);
					ResultSet set = DatasourceUtils.query(sql5_search_plus, list.toArray(new Object[list.size()]));
					table.setModel(new DefaultTableModel(DatasourceUtils.getSetArrays(set), columnNames));
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		jb_find.setFont(new Font("宋体", Font.PLAIN, 15));
		jb_find.setBounds(466, 58, 93, 48);
		panel.add(jb_find);

		JButton jb_message = new JButton("留言");
		jb_message.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MessageWindow instance = MessageWindow.getInstance();
				instance.centerWindow();
				instance.setVisible(true);
			}
		});
		jb_message.setFont(new Font("宋体", Font.PLAIN, 15));
		jb_message.setBounds(566, 58, 93, 48);
		panel.add(jb_message);
		
		JButton jb_add = new JButton("\u65B0\u589E");
		jb_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nameText = jt_name.getText();
				String sexText = jt_sex.getText();
				String ownText = jt_own.getText();
				String timeText = jt_time.getText();
				String idText = jt_id.getText();
				String evalText = jt_eval.getText();
				if ("".equals(nameText) || "".equals(sexText) || "".equals(ownText) || "".equals(timeText) || "".equals(evalText) || "".equals(idText)) {
					JOptionPane.showMessageDialog(null, "信息填写不完整");
				} else {
					try {
						DatasourceUtils.Update(sql2_add, new Object[]{
								nameText, sexText, ownText, timeText, evalText, idText
						});
						init();
						clearAll();
						JOptionPane.showMessageDialog(null, "操作成功！nice");
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});
		jb_add.setBounds(86, 422, 93, 23);
		panel.add(jb_add);
		
		JButton jb_update = new JButton("\u4FEE\u6539");
		jb_update.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String nameText = jt_name.getText();
				String sexText = jt_sex.getText();
				String ownText = jt_own.getText();
				String timeText = jt_time.getText();
				String idText = jt_id.getText();
				String evalText = jt_eval.getText();
				if ("".equals(nameText) || "".equals(sexText) || "".equals(ownText) || "".equals(timeText) || "".equals(evalText) || "".equals(idText)) {
					JOptionPane.showMessageDialog(null, "信息填写不完整");
				} else {
					try {
						DatasourceUtils.Update(sql4_update, new Object[]{
								nameText, sexText, ownText, timeText, evalText, idText,isSelected
						});
						init();
						clearAll();
						JOptionPane.showMessageDialog(null, "操作成功！nice");
					} catch (SQLException ex) {
						throw new RuntimeException(ex);
					}
				}
			}
		});
		jb_update.setBounds(224, 422, 93, 23);
		panel.add(jb_update);
		
		JButton jb_del = new JButton("\u5220\u9664\u884C");
		jb_del.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					DatasourceUtils.Update(sql3_delete, new Object[]{isSelected});
					init();
					JOptionPane.showMessageDialog(null, "操作成功！nice");
				} catch (SQLException ex) {
					throw new RuntimeException(ex);
				}
			}
		});
		jb_del.setBounds(358, 422, 93, 23);
		panel.add(jb_del);
		
		JButton jb_exit = new JButton("\u9000\u51FA");
		jb_exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				new LoginWindow();
			}
		});
		jb_exit.setBounds(488, 422, 93, 23);
		panel.add(jb_exit);
	}
}
